﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;
using System.Configuration;
using System.Net;

namespace Presentation.Mail
{
    public static class EmailSender
    {
        static string from = ConfigurationManager.AppSettings["emailSender"];
        static string password = ConfigurationManager.AppSettings["emailPassword"];
        static string smtp = ConfigurationManager.AppSettings["emailSMTP"];
        static int port = Int32.Parse(ConfigurationManager.AppSettings["emailPortaSMTP"]);
        static int port2 = Int32.Parse(ConfigurationManager.AppSettings["emailPortaSMTP2"]);

        public static void enviar(string assunto, string texto, string emailCliente)
        {
            MailMessage mailMessage = new MailMessage();
            SmtpClient smtpClient = new SmtpClient(smtp);
            try
            {
                mailMessage.From = new MailAddress(from);
                mailMessage.To.Add(from);

                //mailMessage.ReplyToList.Add(new MailAddress(emailCliente));
                mailMessage.ReplyTo = new MailAddress(emailCliente);
                mailMessage.Subject = assunto;
                mailMessage.IsBodyHtml = true;
                mailMessage.Body = texto;

                smtpClient.EnableSsl = true;
                smtpClient.Port = port;
                smtpClient.Credentials = new NetworkCredential(from, password);
                smtpClient.Send(mailMessage);

            }
            catch (Exception)
            {
                try
                {
                    smtpClient.Port = port2;
                    smtpClient.Send(mailMessage);
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
            }
        }
    }
}